package com.atguigu.gmall.mq.controller;


import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.mq.config.DeadLetterMqConfig;
import com.atguigu.gmall.mq.receiver.DelayedMqConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/mq")
public class MqController {

    @Autowired
    private RabbitService rabbitService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 消息发送
     *
     * @return
     */
    @GetMapping("sendConfirm")
    public Result sendConfirm() {
        rabbitService.sendMessage("exchange.confirm", "routing.confirm", "来人了，开始接客吧！");
        return Result.ok();
    }

    /**
     * 发送消息  -- 测试死信队列
     * @return
     */
    @GetMapping("sendDeadLettle")
    public Result sendDeadLettle() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        rabbitTemplate.convertAndSend(DeadLetterMqConfig.exchange_dead,DeadLetterMqConfig.routing_dead_1, "ok");
        System.out.println(simpleDateFormat.format(new Date())+"Delay sent.");
        return Result.ok();
    }


    /**
     * 发送消息 --测试延迟队列
     * @return
     */
    @GetMapping("sendDelay")
    public Result sendDelay() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        rabbitTemplate.convertAndSend(DelayedMqConfig.exchange_delay, DelayedMqConfig.routing_delay,simpleDateFormat.format(new Date()),(msg)->{
            msg.getMessageProperties().setDelay(10 * 1000);
            System.out.println(simpleDateFormat.format(new Date()) + " Delay sent.");
            return msg;
        });
        return Result.ok();
    }
}